package com.example.shixun1.Fragment;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.shixun1.R;
import com.example.shixun1.activity.ArticleDetailActivity;
import com.example.shixun1.adapter.ArticleAdapter;
import com.example.shixun1.model.Article;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class HeadlinesFragment extends Fragment {
    private RecyclerView recyclerView;
    private ArticleAdapter adapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_headlines, container, false);
        recyclerView = view.findViewById(R.id.rv_articles);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        List<Article> articles = loadArticles();
        articles.add(new Article("1", "我国智能机器人场景落地创新加速", "随着人工智能技术的发展...", "央视网", new Date()));
        articles.add(new Article("2", "一草一木染出惊艳“中国色”", "色彩，是人类最直观、最通用的视觉信号...", "北青网", new Date()));

        adapter = new ArticleAdapter(articles,
                article -> {
                    // 点击文章查看详情
                    Intent intent = new Intent(getActivity(), ArticleDetailActivity.class);
                    intent.putExtra("article", article);
                    startActivity(intent);
                },
                article -> {
                    // 删除文章回调
                    deleteArticle(article);
                });

        recyclerView.setAdapter(adapter);
        return view;
    }

    private void deleteArticle(Article article) {
        SharedPreferences.Editor editor = requireContext()
                .getSharedPreferences("articles", MODE_PRIVATE)
                .edit();

        // 删除与该文章相关的所有键
        editor.remove(article.getId() + "_title");
        editor.remove(article.getId() + "_content");
        editor.remove(article.getId() + "_author");
        editor.remove(article.getId() + "_time");

        editor.apply();

        Toast.makeText(getContext(), "文章已删除", Toast.LENGTH_SHORT).show();
    }
    private List<Article> loadArticles() {
        List<Article> articles = new ArrayList<>();
        SharedPreferences prefs = getContext().getSharedPreferences("articles", MODE_PRIVATE);

        // 获取所有以"article_"开头的key
        Map<String, ?> allEntries = prefs.getAll();
        for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
            if (entry.getKey().endsWith("_title")) {
                String id = entry.getKey().replace("_title", "");
                String title = (String) entry.getValue();
                String content = prefs.getString(id + "_content", "");
                String author = prefs.getString(id + "_author", "未知作者");
                long time = prefs.getLong(id + "_time", System.currentTimeMillis());

                articles.add(new Article(id, title, content, author, new Date(time)));
            }
        }
        return articles;
    }
}